package com.example.administrator.social.fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.administrator.social.R;
import com.example.administrator.social.activitys.CharActivity;
import com.example.administrator.social.adapter.MessageRecyclerAdapter;
import com.example.administrator.social.model.MessageData;
import com.example.administrator.social.model.User;
import com.example.administrator.social.util.PinYinUtils;
import com.example.administrator.social.util.PinyinComparator;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
 * A simple {@link Fragment} subclass.
 */
public class  MessageFragment extends Fragment implements MessageRecyclerAdapter.IonSlidingViewClickListener {

    SmartRefreshLayout mRefreshLayout;
    RecyclerView mRecyclerView;
    private Context mContext;
    private WaterDropHeader mWaterDropHeader;
    private static ArrayList<MessageData> mData;
    PinyinComparator mPinyinComparator;
    MessageRecyclerAdapter mRecyclerAdapter;

    static{
        mData = new ArrayList<MessageData>();
        for (int i=0;i<20;i++){
            SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm");
            String dateStr = dateformat.format(System.currentTimeMillis());
            MessageData message = new MessageData(i,"allen"+i,R.drawable.header_image,""+dateStr,"这是第"+i+"个信息");
            //汉字转换成拼音
            String pinyin = PinYinUtils.getPingYin(message.getName());
            String sortString = pinyin.substring(0, 1).toUpperCase();
            // 正则表达式，判断首字母是否是英文字母
            if (sortString.matches("[A-Z]")) {
                message.setLetters(sortString.toUpperCase());
            } else {
                message.setLetters("#");
            }
            mData.add(message);
        }
    }

    public MessageFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        mContext = getContext();
        mPinyinComparator = new PinyinComparator();
        mRefreshLayout = (SmartRefreshLayout) view.findViewById(R.id.smart_refresh_layout);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.message_recycler);

        initRecyclerView();
        initRefreshLayout();
        return view;
    }

    private void initRefreshLayout() {
        //设置 Header 为 苹果水滴 样式
        mWaterDropHeader =   new WaterDropHeader(mContext);
        //mRefreshLayout.setRefreshHeader(mWaterDropHeader);
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        //设置 Footer 为 球脉冲 样式
        mRefreshLayout.setRefreshFooter(new BallPulseFooter(mContext).setSpinnerStyle(SpinnerStyle.Scale));
        mRefreshLayout.setOnRefreshLoadmoreListener(mRefreshListener);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        mRecyclerAdapter = new MessageRecyclerAdapter(mContext,mData,this);

        //add Split Line
        DividerItemDecoration divider = new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(mContext,R.drawable.message_recycler_divider));
        mRecyclerView.addItemDecoration(divider);
        //Set CallBack
        mRecyclerAdapter.setCallBack(new MessageRecyclerAdapter.MessageCallBack() {
            @Override
            public void filterDataForList(String s) {
                filterData(s);
            }
        });
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }

    /**
     * filter the dataList
     * @param filterStr
     */
    private void filterData(String filterStr) {
        List<MessageData> filterDateList = new ArrayList<>();

        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = mData;
        } else {
            filterDateList.clear();
            for (MessageData sortModel : mData) {
                String name = sortModel.getName();
                if (name.indexOf(filterStr.toString()) != -1 ||
                        PinYinUtils.getFirstSpell(name).startsWith(filterStr.toString())
                        //不区分大小写
                        || PinYinUtils.getFirstSpell(name).toLowerCase().startsWith(filterStr.toString())
                        || PinYinUtils.getFirstSpell(name).toUpperCase().startsWith(filterStr.toString())
                        ) {
                    filterDateList.add(sortModel);
                }
            }
        }
        // 根据a-z进行排序
        Collections.sort(filterDateList, mPinyinComparator);
        mRecyclerAdapter.updateList(filterDateList);
    }


    /**
     * the refresh listener
     */
    private OnRefreshLoadmoreListener mRefreshListener = new OnRefreshLoadmoreListener() {
        /**
         * FootView Refresh
         * @param refreshlayout
         */
        @Override
        public void onLoadmore(RefreshLayout refreshlayout) {

        }

        /**
         * HeaderView Refresh
         * @param refreshlayout
         */
        @Override
        public void onRefresh(RefreshLayout refreshlayout) {
            refreshlayout.finishRefresh(2000);
        }
    };


    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(mContext,CharActivity.class);
        /*
        * 携带数据
        * */
        intent.putExtra("userName",mData.get(position).getName());
        startActivity(intent);
    }

    @Override
    public void onDeleteBtnClick(View view, int position) {
        mRecyclerAdapter.removeData(position);
    }
}
